﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Utils.ControlToLoad;
using Utils.Exceptions;
using Core;
using Core.DAL;

namespace xManagerWebApp.EditControls
{
    public partial class ControlTemplateSimple : System.Web.UI.UserControl, IControlToLoadRW
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnPreRender(EventArgs e)
        {
            if (this.ReadOnly)
                this.SetFieldsReadOnly();
        }

        private void SetFieldsReadOnly()
        {
            this.tb_Value.Enabled = false;
        }

        #region IControlToLoad Members


        public bool ReadOnly
        {
            get
            {
                if (ViewState["ReadOnly"] != null)
                    return (bool)ViewState["ReadOnly"];
                else
                    return false;
            }
            set { ViewState["ReadOnly"] = value; }
        }

        public Guid? Id
        {
            get
            {
                if (ViewState["Id1"] != null)
                    return (Guid?)ViewState["Id1"];
                else
                    return null;
            }
            set { ViewState["Id1"] = value; }
        }

        public void Save()
        {
            ExpenseLineSimple exp = new ExpenseLineSimple();
            exp.Id = this.Id.Value;
            exp.Value = Convert.ToDecimal(this.tb_Value.Text);
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                exp.Save(dal);
            }
        }
        #endregion

        #region IControlToLoadRW Members

        public Guid? TypeId
        {
            get
            {
                if (ViewState["TypeId"] != null)
                    return (Guid?)ViewState["TypeId"];
                else
                    throw new ValueNotAvailableException();
            }
            set { ViewState["TypeId"] = value; }
        }

        public void Bind(Guid Id, Guid TypeId)
        {
            this.Id = Id;
            this.TypeId = TypeId;
        }

        #endregion

        private void LoadFields(ExpenseLineSimple e)
        {
            this.tb_Value.Text = Convert.ToString(e.Value);
        }

        #region IControlToLoadRW Members


        public void BindControls(Guid id)
        {
            this.Id = id;
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                ExpenseLineSimple e = ExpenseLineSimple.Get(dal, this.Id.Value);
                if (e != null)
                    this.LoadFields(e);
            }
        }

        public void BindType(Guid TypeId)
        {
            this.TypeId = TypeId;
        }

        public void ClearFields()
        {
            this.tb_Value.Text = string.Empty;
        }

        #endregion
    }
}